/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean.TX.Dashboards;

import entity.Outlet;
import entity.Stakeholder;
import entity.StakeholderFood;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.submenu.Submenu;
import org.primefaces.model.DefaultMenuModel;
import org.primefaces.model.MenuModel;
import session.TX.OutletSBRemote;
import session.TX.StakeholderAccountSBRemote;

/**
 *
 * @author Lu Xiaodi
 */
@ManagedBean(name = "ttaDashboardMB")
@ViewScoped
public class TTADashboardMB {

    private MenuModel ttaMenu;
    private String stakeholderName;
    private Long outletId;
    private Long stakeholderId;
    private String accountType;
    private Long accountId;
    @EJB
    private StakeholderAccountSBRemote stakeholderManager;
    @EJB
    private OutletSBRemote outletBean;

    /** Creates a new instance of TTADashboardMB */
    public TTADashboardMB() {
    }

    public void initView(PhaseEvent event) {
        System.out.println("tta dashboard init...");
        try {
            accountType = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("accountType");
            if (accountType != null) {
                accountId = Long.valueOf(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("accountId").toString());

                if (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId") != null) {
                    stakeholderId = Long.valueOf(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderId").toString());
                    stakeholderName = (FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString());
                }
                //getAccountRoles();
                generateDashboard();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }//initView

    public void generateDashboard() {
        System.out.println("account type   >>>> " + accountType);
        ttaMenu = new DefaultMenuModel();
        Submenu submenu = new Submenu();
        MenuItem item = new MenuItem();

        //Tx Team TCube Admin Navigation Links
        if (accountType.compareTo("admin") == 0) {
            System.out.println(">>> DashboardMB: Generating Dashboard for TCube Admin");

            submenu = new Submenu();
            submenu.setLabel("Performance Index Prediction");

            item = new MenuItem();
            item.setValue("Performance Index and Prediction");
            item.setUrl("./pip.xhtml");
            submenu.getChildren().add(item);

            ttaMenu.addSubmenu(submenu);

            submenu = new Submenu();
            submenu.setLabel("Popularity Analysis");

            item = new MenuItem();
            item.setValue("Outlet Popularity");
            item.setUrl("./popularity.xhtml");
            submenu.getChildren().add(item);

            ttaMenu.addSubmenu(submenu);

            submenu = new Submenu();
            submenu.setLabel("Marketing Decision and Support");

            item = new MenuItem();
            item.setValue("Overall");
            item.setUrl("./mds.xhtml");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Sales By Age Group");
            item.setUrl("./salebyage.xhtml");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Sales By Country");
            item.setUrl("./salebycountry.xhtml");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Reservations By Age Group");
            item.setUrl("./resbyage.xhtml");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Reservations By Country");
            item.setUrl("./resbycountry.xhtml");
            submenu.getChildren().add(item);

            item = new MenuItem();
            item.setValue("Transactions By Demographic Profile");
            item.setUrl("./cluster.xhtml");
            submenu.getChildren().add(item);

            ttaMenu.addSubmenu(submenu);

        }

        //---------------------------------------------------------------------------------------------------------------------------------------------------------
        //---------------------------------------------------------------------------------------------------------------------------------------------------------

        //Tx Team Stakeholder Representative Navigation Links
        if (accountType.compareTo("representative") == 0) {

            System.out.println(">>> DashboardMB: Generating Dashboard for Stakeholder Representative");
            stakeholderName = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("stakeholderName").toString();
            Stakeholder stkhldr = stakeholderManager.findStakeholderByName(stakeholderName);

            //**********   FOOD STAKEHOLDER MENU
            if (stkhldr.getClass() == StakeholderFood.class) {
                //pip
                submenu = new Submenu();
                submenu.setLabel("Performance Index & Prediction");

                item = new MenuItem();
                item.setValue("Performance Index & Prediction");
                item.setUrl("./pip.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);

                // Cust Value Analysis
                submenu = new Submenu();
                submenu.setLabel("Customer Value Analysis");

                item = new MenuItem();
                item.setValue("Local");
                item.setUrl("./valueanalysis_local.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Foreign");
                item.setUrl("./valueanalysis_foreign.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);

                // Cust Loyalty Analysis
                submenu = new Submenu();
                submenu.setLabel("Cust Loyalty Analysis");

                item = new MenuItem();
                item.setValue("Local");
                item.setUrl("./loyaltyanalysis_local.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Foreign");
                item.setUrl("./loyaltyanalysis_foreign.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);
                
                // Cust Loyalty Analysis
                submenu = new Submenu();
                submenu.setLabel("Cust Loyalty Analysis");

                item = new MenuItem();
                item.setValue("CLV Calculation");
                item.setUrl("./clv.xhtml");
                submenu.getChildren().add(item);
                
                ttaMenu.addSubmenu(submenu);

                // Marketing Decision and Support
                submenu = new Submenu();
                submenu.setLabel("Customer Lifetime Value");
                
                item = new MenuItem();
                item.setValue("Overall");
                item.setUrl("./mds.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales By Age Group");
                item.setUrl("./saleByAge.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales By Country");
                item.setUrl("./salebycountry.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Reservations By Age Group");
                item.setUrl("./resbyage.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Reservations By Country");
                item.setUrl("./resbycountry.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Transactions By Demographic Profile");
                item.setUrl("./cluster.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu); 
            }//End Menu Generation code for representative

            //---------------------------------------------------------------------------------------------------------------------------------------------------------
            //---------------------------------------------------------------------------------------------------------------------------------------------------------
        }

        //Tx Team Outlet Manager Navigation Links
        if (accountType.compareTo("foodOutletManager") == 0) {
            System.out.println(">>> DashboardMB: Generating Dashboard for Outlet Manager TTA");
            outletId = (Long) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("outletId");
            Outlet outlet = outletBean.getOutlet(outletId);

            //**********   FOOD STAKEHOLDER MENU
            if (outlet.getClass() == Outlet.class) {

                //pip
                submenu = new Submenu();
                submenu.setLabel("Performance Index & Prediction");

                item = new MenuItem();
                item.setValue("Performance Index & Prediction");
                item.setUrl("./pip.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);

                // Cust Value Analysis
                submenu = new Submenu();
                submenu.setLabel("Customer Value Analysis");

                item = new MenuItem();
                item.setValue("Local");
                item.setUrl("./valueanalysis_local.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Foreign");
                item.setUrl("./valueanalysis_foreign.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);

                // Cust Loyalty Analysis
                submenu = new Submenu();
                submenu.setLabel("Cust Loyalty Analysis");

                item = new MenuItem();
                item.setValue("Local");
                item.setUrl("./loyaltyanalysis_local.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Foreign");
                item.setUrl("./loyaltyanalysis_foreign.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);

                // Marketing Decision and Support
                submenu = new Submenu();
                submenu.setLabel("Marketing Decision and Support");

                item = new MenuItem();
                item.setValue("Overall");
                item.setUrl("./mds.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales By Age Group");
                item.setUrl("./saleByAge.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Sales By Country");
                item.setUrl("./salebycountry.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Reservations By Age Group");
                item.setUrl("./resbyage.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Reservations By Country");
                item.setUrl("./resbycountry.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Transactions By Demographic Profile");
                item.setUrl("./cluster.xhtml");
                submenu.getChildren().add(item);

                item = new MenuItem();
                item.setValue("Dish Sale");
                item.setUrl("./dishsale.xhtml");
                submenu.getChildren().add(item);

                ttaMenu.addSubmenu(submenu);
            }//End Menu Generation code for outlet manager

            //---------------------------------------------------------------------------------------------------------------------------------------------------------
            //---------------------------------------------------------------------------------------------------------------------------------------------------------       
        }
    }

    public MenuModel getTtaMenu() {
        return ttaMenu;
    }

    public void setTtaMenu(MenuModel ttaMenu) {
        this.ttaMenu = ttaMenu;
    }
}